package com.xq.hibernate;

import com.xq.hibernate.entity.Event;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import java.util.Date;
import java.util.List;

/**
 * 功能说明: 第一个Hibernate 原生接口的测试类.
 * <p>创建时间: 2018-10-17 18:38</p>
 *
 * @author 郭凯强
 * @version 1.0-SNAPSHOT
 */
public class NativeApiTest01 extends BaseNativeApiTest {
    @Test
    public void testBasicUsage() {
        // create a couple of events...
        Session session = getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        session.save(new Event("Our very first event!", new Date()));
        session.save(new Event("A follow up event", new Date()));
        transaction.commit();
        session.close();

        // now lets pull events from the database and list them
        session = getSessionFactory().openSession();
        transaction = session.beginTransaction();
        List<Event> result = session.createQuery("from Event",Event.class).list();
        result.forEach(System.out::println);
        transaction.commit();
        session.close();
    }
}
